﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace LaneLpnr
{
    class udpc
    {
        Common.WriteLog trace = new Common.WriteLog();
   
        private UdpClient myUdpClient = null;

        public int localPort = 10040;

        byte[] senddata;

        //一号车道启动消息发送
        public void StartUdpSend(byte[] buf)
        {
            try
            {
                senddata = new byte[buf.Length];
                Array.Copy(buf, senddata, buf.Length);

                SendMsg();
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常StartUdpSend" + ex.ToString());
            }
            //Thread SendMsgThread = new Thread(SendMsg);
            //SendMsgThread.IsBackground = true;
            //SendMsgThread.Start();
        }

        //一号车道发送消息线程
        private void SendMsg()
        {
            try
            {
                if (myUdpClient != null)
                {
                    IPEndPoint remote = new IPEndPoint(IPAddress.Parse(Common.ConfigHelper.GetConnectionStringConfig("LaneIp_1")), 10041);
                    myUdpClient.Send(senddata, senddata.Length, remote);

                }
                else
                {
                    FlashLog.FlashLogger.Info("异常未将udpclient 实例化");
                }
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常SendMsg" + ex.ToString());
            }

        }


        //二号号车道启动消息发送
        public void StartUdpSend_2(byte[] buf)
        {
            senddata = new byte[buf.Length];
            Array.Copy(buf, senddata, buf.Length);

            Thread SendMsgThread = new Thread(SendMsg_2);
            SendMsgThread.IsBackground = true;
            SendMsgThread.Start();

        }
        //二号车道发送消息线程
        private void SendMsg_2()
        {
            try
            {
                if (myUdpClient != null)
                {
                    IPEndPoint remote = new IPEndPoint(IPAddress.Parse(Common.ConfigHelper.GetConnectionStringConfig("LaneIp_2")), 10041);
                    myUdpClient.Send(senddata, senddata.Length, remote);
                }
                else
                {
                    FlashLog.FlashLogger.Info("未将udpclient 实例化");
                }
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常SendMsg_2" + ex.ToString());
            }
        }


        public bool startUdpBit = false;
        public static Socket server;

        public void StartUdpReceive()
        {
            try
            {
                if (!startUdpBit)
                {
                    //IPEndPoint localPoint = new IPEndPoint(IPAddress.Any, 10041);

                    myUdpClient = new UdpClient(10041);//用于指定UDP服务再本地计算机的端口上接收数据
                    startUdpBit = true;
                    FlashLog.FlashLogger.Info("Udp开启");
                }
                else
                {
                    FlashLog.FlashLogger.Info("udp已经开启，无需再使能");
                }


                myUdpClient.BeginReceive(new AsyncCallback(ReceiveMsgCallback), null);
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常StartUdpReceive" + ex.ToString());
            }
            //Thread ReceMsgThread = new Thread(ReceiveMsg);
            //ReceMsgThread.IsBackground = true;
            //ReceMsgThread.Start();
        }

        private void ReceiveMsgCallback(IAsyncResult ar)
        {
            try
            {
                if (myUdpClient != null)
                {
                    try
                    {
                        IPEndPoint remoteHost = new IPEndPoint(IPAddress.Any, 0);

                        byte[] buffer = myUdpClient.EndReceive(ar, ref remoteHost);

                        string msg = BitConverter.ToString(buffer);

                        string str = string.Format("从{0}接收到消息{1}", remoteHost.ToString(), msg);
                        FlashLog.FlashLogger.Info("Udp接收到 " + str);
                    }
                    catch (Exception ex)
                    {
                        FlashLog.FlashLogger.Info("异常ReceiveMsgCallback " + ex.ToString());
                    }

                }
                if (myUdpClient != null)
                {
                    try
                    {
                        myUdpClient.BeginReceive(new AsyncCallback(ReceiveMsgCallback), null);
                    }
                    catch (Exception ex)
                    {
                        FlashLog.FlashLogger.Info("异常ReceiveMsgCallback " + ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常ReceiveMsgCallback" + ex.ToString());
            }
        }




        //显示消息的被委托的方法
        private void showmsg(string text)
        {
            FlashLog.FlashLogger.Info(text);
        }
        /// <summary>
        ///消息接收线程
        /// </summary>
        public void ReceiveMsg()
        {
            FlashLog.FlashLogger.Info("udp接受线程开启");
            IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 0);

            try
            {
                while (true)
                {
                    byte[] getdata;
                    getdata = myUdpClient.Receive(ref iPEndPoint);//任何ip发送过来的都接受
                    string msg = CommonData.byteToHexStr(getdata);
                    FlashLog.FlashLogger.Info(msg);
                }

            }
            catch (ThreadAbortException)
            {

            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常ReceiveMsg" + ex.Message.ToString());
            }
        }
    }
}
